package com.xiaoyu.lanling.feature.chat.viewholder

import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.emoji.widget.EmojiTextView
import com.facebook.drawee.view.SimpleDraweeView
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.feature.chat.handler.ChatMessageAudioItemClickHandler
import com.xiaoyu.lanling.feature.chat.model.message.ChatMessageReceiveAudioItem
import com.xiaoyu.lanling.media.image.ImageViewUtil

class ChatMessageReceiveAudioViewHolder : ChatMessageBaseReceiveSendViewHolder<ChatMessageReceiveAudioItem>() {

    private var mAudio: TextView? = null
    private var mNewTip: View? = null
    private var mRewardLayout: View? = null
    private var mRewardIcon: SimpleDraweeView? = null
    private var mRewardDesc: EmojiTextView? = null

    override fun doCreateChildrenView(layoutInflater: LayoutInflater, parent: ViewGroup): View? {
        val view = layoutInflater.inflate(R.layout.item_chat_receive_audio, parent, false)
        mAudio = view?.findViewById(R.id.chat_message_audio)
        mNewTip = view?.findViewById(R.id.chat_message_new_tip)
        mRewardLayout = view.findViewById(R.id.reward_layout)
        mRewardIcon = view.findViewById(R.id.reward_icon)
        mRewardDesc = view.findViewById(R.id.reward_desc)
        return view
    }

    override fun showData(position: Int, itemData: ChatMessageReceiveAudioItem) {
        super.showData(position, itemData)
        mRewardLayout?.visibility = if (TextUtils.isEmpty(itemData.rewardInfo.desc)) View.GONE else View.VISIBLE
        mRewardDesc?.text = itemData.rewardInfo.desc
        mRewardDesc?.setTextColor(itemData.rewardInfo.textColor)
        ImageViewUtil.loadImage(mRewardIcon, itemData.rewardInfo.iconloadParam)

        if (itemData.singleChat) {
            mUserDisplayName?.visibility = View.GONE
        } else {
            mUserDisplayName?.visibility = View.VISIBLE
            mUserDisplayName?.text = itemData.displayName
        }
        val audioDuration = "    " + itemData.duration
        mAudio?.text = audioDuration
        mNewTip?.visibility = if (itemData.newTip) View.VISIBLE else View.GONE

        mAudio?.let {
            ChatMessageAudioItemClickHandler.bindReceiveAudioItemClickForViewAndData(it, itemData);
            ChatMessageAudioItemClickHandler.bindReceiveAudioItemLongClickForViewAndData(it, itemData);
        }
        processAutoPlay(itemData)
    }

    private fun processAutoPlay(item: ChatMessageReceiveAudioItem) {
        if (!item.mAutoPlay) {
            return
        }
        mAudio?.performClick()
    }
}